import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
var _excluded = ["__id", "_status"],
    _excluded2 = ["__id", "_status"],
    _excluded3 = ["__id", "_status"];

/*
 * WebHookConfigDS WebHook配置DS
 * @date: 2020-04-28
 */
import intl from 'utils/intl';
import { CODE } from 'utils/regExp';
import { isTenantRoleLevel, getCurrentOrganizationId } from 'utils/utils';
import notification from 'utils/notification';
import { CPAAS_MSG } from 'utils/config';
var organizationId = getCurrentOrganizationId();
var isTenant = isTenantRoleLevel();
var apiPrefix = isTenant ? "".concat(CPAAS_MSG, "/v1/").concat(organizationId) : "".concat(CPAAS_MSG, "/v1"); // 表格ds

var tableDS = {
  autoQuery: true,
  selection: false,
  dataKey: 'content',
  queryFields: [!isTenant && {
    name: 'tenantLov',
    lovCode: 'CPFM.TENANT',
    type: 'object',
    label: intl.get('cpaas.common.model.common.tenantId').d('租户'),
    ignore: 'always',
    noCache: true
  }, !isTenant && {
    name: 'tenantId',
    type: 'string',
    bind: 'tenantLov.tenantId'
  }, {
    name: 'serverCode',
    type: 'string',
    label: intl.get('cmsg.common.view.serverCode').d('配置编码')
  }, {
    name: 'serverName',
    type: 'string',
    label: intl.get('cmsg.common.view.serverName').d('配置名称')
  }].filter(Boolean),
  fields: [!isTenant && {
    name: 'tenantName',
    label: intl.get('cpaas.common.model.tenantName').d('租户'),
    type: 'string'
  }, {
    name: 'serverCode',
    label: intl.get('cmsg.common.view.serverCode').d('配置编码'),
    type: 'string'
  }, {
    name: 'serverName',
    type: 'string',
    label: intl.get('cmsg.common.view.serverName').d('配置名称')
  }, {
    name: 'serverTypeMeaning',
    type: 'string',
    label: intl.get('cmsg.common.view.type').d('类型')
  }, {
    name: 'description',
    type: 'string',
    label: intl.get('cmsg.webhookConfig.model.webhookConfig.description').d('描述')
  }, {
    name: 'tenantId',
    type: 'string',
    label: intl.get('cmsg.common.view.source').d('来源')
  }, {
    name: 'enabledFlag',
    type: 'number',
    label: intl.get('cpaas.common.status.enable').d('启用')
  }].filter(Boolean),
  transport: {
    read: function read() {
      return {
        url: "".concat(apiPrefix, "/webhook-servers"),
        method: 'GET'
      };
    },
    destroy: function destroy(_ref) {
      var data = _ref.data;

      var _ref2 = Array.isArray(data) ? data[0] : {},
          __id = _ref2.__id,
          _status = _ref2._status,
          other = _objectWithoutProperties(_ref2, _excluded);

      return {
        data: other,
        url: "".concat(apiPrefix, "/webhook-servers"),
        method: 'DELETE'
      };
    }
  }
}; // 新建或编辑时的DS

var drawerDS = {
  autoQuery: false,
  autoQueryAfterSubmit: false,
  fields: [!isTenant && {
    name: 'tenantLov',
    lovCode: 'CPFM.TENANT',
    required: true,
    type: 'object',
    label: intl.get('cpaas.common.model.common.tenantId').d('租户'),
    ignore: 'always',
    noCache: true
  }, !isTenant && {
    name: 'tenantId',
    type: 'string',
    bind: 'tenantLov.tenantId'
  }, !isTenant && {
    name: 'tenantName',
    type: 'string',
    bind: 'tenantLov.tenantName'
  }, {
    name: 'serverCode',
    required: true,
    type: 'string',
    maxLength: 30,
    label: intl.get('cmsg.common.view.serverCode').d('配置编码'),
    pattern: CODE,
    defaultValidationMessages: {
      patternMismatch: intl.get('cpaas.common.validation.code').d('大小写及数字，必须以字母、数字开头，可包含“-”、“_”、“.”、“/”')
    }
  }, {
    name: 'serverName',
    type: 'string',
    required: true,
    label: intl.get('cmsg.common.view.serverName').d('配置名称'),
    maxLength: 60
  }, {
    name: 'serverType',
    type: 'string',
    label: intl.get('cmsg.common.view.type').d('类型'),
    lookupCode: 'CMSG.WEBHOOK_TYPE',
    required: true
  }, {
    name: 'webhookAddress',
    type: 'url',
    required: true,
    label: intl.get('cmsg.webhookConfig.model.webhookConfig.webhookAddress').d('WebHook地址'),
    maxLength: 480,
    dynamicProps: {
      required: function required(_ref3) {
        var record = _ref3.record;
        return record.get('isCreate');
      }
    }
  }, {
    name: 'secret',
    type: 'string',
    label: intl.get('cmsg.webhookConfig.model.webhookConfig.secret').d('秘钥'),
    maxLength: 240
  }, {
    name: 'description',
    type: 'string',
    label: intl.get('cmsg.webhookConfig.model.webhookConfig.description').d('描述'),
    maxLength: 240
  }, {
    name: 'enabledFlag',
    type: 'boolean',
    required: true,
    label: intl.get('cpaas.common.status.enable').d('启用'),
    trueValue: 1,
    falseValue: 0,
    defaultValue: 1
  }, {
    name: 'isCreate',
    // 用于判断字段是否必输
    type: 'boolean',
    defaultValue: true,
    ignore: 'always'
  }].filter(Boolean),
  transport: {
    read: function read(_ref4) {
      var data = _ref4.data;
      var serverId = data.serverId;
      return {
        url: "".concat(apiPrefix, "/webhook-servers/").concat(serverId),
        method: 'GET',
        data: {},
        params: {}
      };
    },
    create: function create(_ref5) {
      var data = _ref5.data;

      var _ref6 = Array.isArray(data) ? data[0] : {},
          __id = _ref6.__id,
          _status = _ref6._status,
          other = _objectWithoutProperties(_ref6, _excluded2);

      return {
        url: "".concat(apiPrefix, "/webhook-servers"),
        method: 'POST',
        data: other
      };
    },
    update: function update(_ref7) {
      var data = _ref7.data;

      var _ref8 = Array.isArray(data) ? data[0] : {},
          __id = _ref8.__id,
          _status = _ref8._status,
          other = _objectWithoutProperties(_ref8, _excluded3);

      return {
        url: "".concat(apiPrefix, "/webhook-servers"),
        method: 'PUT',
        data: other
      };
    }
  },
  feedback: {
    submitFailed: function submitFailed(error) {
      if (error && error.failed) {
        notification.error({
          message: error.message
        });
      }
    }
  }
};
export { tableDS, drawerDS };